import indexer.CrawlingManager;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.WindowConstants;

import org.jdesktop.application.Application;

public class MainJFrame extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1867138238136692176L;
	private JPanel buttonsPanel;
	private JPanel contentPanel;
	private JTextArea errLogTA;
	private JTextArea reportTA;
	private JButton stopButton;
	private JButton startButton;

	CrawlingManager _crawlingManager = new CrawlingManager();;

	public MainJFrame() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setLayout(null);

			this.setResizable(false);
			{
				buttonsPanel = new JPanel();
				getContentPane().add(buttonsPanel);
				buttonsPanel.setBounds(0, 0, 872, 88);
				buttonsPanel.setLayout(null);
				{
					startButton = new JButton();
					buttonsPanel.add(startButton);
					startButton.setBounds(12, 30, 111, 23);
					startButton.setName("startButton");
					startButton.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {
							startCrawling();
						}
					});
				}
				{
					stopButton = new JButton();
					buttonsPanel.add(stopButton);
					stopButton.setBounds(143, 30, 99, 23);
					stopButton.setName("stopButton");
					stopButton.addActionListener(new ActionListener() {

						@Override
						public void actionPerformed(ActionEvent e) {

							stopCrawling();
						}
					});
				}
			}
			{
				contentPanel = new JPanel();
				getContentPane().add(contentPanel);
				contentPanel.setBounds(0, 94, 872, 599);
				contentPanel.setLayout(null);
				{
					reportTA = new JTextArea();
					reportTA.setBounds(7, 5, 853, 328);
					reportTA.setEditable(false);
					JScrollPane reportSP = new JScrollPane(reportTA,
							JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					reportSP.setBounds(reportTA.getBounds());
					contentPanel.add(reportSP);
				}
				{
					errLogTA = new JTextArea();
					errLogTA.setEditable(false);
					errLogTA.setBounds(7, 351, 851, 230);
					JScrollPane errLogSP = new JScrollPane(errLogTA,
							JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
							JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					errLogSP.setBounds(errLogTA.getBounds());
					contentPanel.add(errLogSP);
				}

			}

			this.setSize(878, 721);
			this.setVisible(true);
			Application.getInstance().getContext().getResourceMap(getClass())
					.injectComponents(getContentPane());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * method startCrawling starts all the crawler threads
	 */
	public void startCrawling() {
		_crawlingManager.StartSpider();
	}

	/**
	 * method stopCrawling starts all the crawler threads
	 */
	public void stopCrawling() {
		_crawlingManager.StopSpiders();
	}

	public synchronized void printToReportTextPane(String str) {
		reportTA.append(str + "\n");

	}

	public synchronized void printToErrLogTextPane(String str) {
		errLogTA.append(str + "\n");

	}

}
